package com.mgl.movesprite;

import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import com.mgl.movesprite.game.factory.GameObjectFactory;
import com.mgl.movesprite.game.factory.GameType;
import com.mgl.movesprite.game.model.Level;
import com.mgl.util.ButtonSpriteListener;
import com.mgl.util.Point;

public abstract class EntitySpriteLevelBase extends Sprite implements IEntityLevel {

	protected ButtonSpriteListener buttonSpriteListener = null;
	protected GameObjectFactory gameObjectFactory;
	protected GameType type;
	
	public EntitySpriteLevelBase(float pX, float pY, ITextureRegion pTextureRegion,
			VertexBufferObjectManager pVertexBufferObjectManager,
			GameObjectFactory gameObjectFactory) {
		//center
		super(pX-pTextureRegion.getWidth()/2, pY-pTextureRegion.getHeight()/2, pTextureRegion, pVertexBufferObjectManager);
		//super(pX, pY, pTextureRegion, pVertexBufferObjectManager);
		this.gameObjectFactory = gameObjectFactory;
		
	}

	@Override
	public abstract void update(float pSecondsElapsed, Level level);

	public GameObjectFactory getGameObjectFactory() {
		
		return gameObjectFactory;
	}

	public void setGameObjectFactory(GameObjectFactory gameObjectFactory) {
		this.gameObjectFactory = gameObjectFactory;
	}
	
	@Override
	public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
			final float pTouchAreaLocalX, final float pTouchAreaLocalY) {

		// Toggle action
		switch (pSceneTouchEvent.getAction()) {
		case TouchEvent.ACTION_DOWN:
			if (buttonSpriteListener != null) {
				buttonSpriteListener.onTouchDown();
			}
			break;
		case TouchEvent.ACTION_MOVE:
			if (buttonSpriteListener != null) {
				buttonSpriteListener.onTouchMove();
			}
			break;
		case TouchEvent.ACTION_UP:
			if (buttonSpriteListener != null) {
				buttonSpriteListener.onTouchUp();
			}
			break;
		}
		return true;
	}

	public Point getCenterPoint(){
		
		return new Point(this.getWidth()/2, this.getHeight()/2); 
	}
	
	public ButtonSpriteListener getButtonSpriteListener() {
		return buttonSpriteListener;
	}

	public void setButtonSpriteListener(ButtonSpriteListener buttonSpriteListener) {
		this.buttonSpriteListener = buttonSpriteListener;
	}

	public GameType getType() {
		return type;
	}

	public void setType(GameType type) {
		this.type = type;
	}

	@Override
	public void detachFromScene(Scene scene) {
		scene.detachChild(this);
		
	}

	@Override
	public void attachToScene(Scene scene) {
		scene.attachChild(this);
		
	}	
	
}
